package hlhj.fhp.tvlib.activitys

import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.AnimationDrawable
import android.media.AudioManager
import android.media.MediaPlayer
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import android.widget.Toast
import com.bumptech.glide.Glide
import hlhj.fhp.tvlib.Contents
import hlhj.fhp.tvlib.R
import hlhj.fhp.tvlib.fragment.HomeCastNew
import kotlinx.android.synthetic.main.new_case_detail.*

class CastDetailAty : BaseAty(), MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnPreparedListener {
    private var url=""
    private var thumb=""
    private var tittle=""
    private var isPlaying=false
    private var isLoaded=false
    private var p=0
    private var volume=0
    val animDrawable1= AnimationDrawable()
    val animDrawable2= AnimationDrawable()
    val animDrawable3= AnimationDrawable()
    val animDrawable4= AnimationDrawable()
    val animDrawable5= AnimationDrawable()

    private var  mAudioManager:AudioManager?=null
    private val handler= @SuppressLint("HandlerLeak")

    object :Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            volume = mAudioManager!!.getStreamVolume(AudioManager.STREAM_MUSIC)

            Log.e("fhp","来了来了${volume}")
            this.sendEmptyMessageDelayed(1,20)
        }
    }
    private lateinit var animator:ObjectAnimator
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        p=intent.getIntExtra("p",0)

        url=HomeCastNew.datas[p].radio_source
        thumb=HomeCastNew.datas[p].radio_thumb
        tittle=HomeCastNew.datas[p].radio_name
        Glide.with(this).load(Contents.BaseUrl+intent.getStringExtra("head"))
                .into(imgBg)

        supportActionBar!!.hide()
        initAnim()
        initPlayer()
        initListener()

        initVoice()
    }

    private fun initVoice() {
        mAudioManager=getSystemService(Context.AUDIO_SERVICE) as AudioManager


    }

    override fun getContentId(): Int {
        return R.layout.new_case_detail
    }

    private fun initAnim() {
        animator= ObjectAnimator.ofFloat(itemImg,"rotation",0f,360f)
        animator.duration=5000
        animator.interpolator=null
        animator.repeatCount=-1

        animDrawable1.addFrame(resources.getDrawable(R.drawable.bv1),100)
        animDrawable1.addFrame(resources.getDrawable(R.drawable.bv2),100)
        animDrawable1.addFrame(resources.getDrawable(R.drawable.bv3),100)
        animDrawable1.addFrame(resources.getDrawable(R.drawable.bv4),100)
        animDrawable1.addFrame(resources.getDrawable(R.drawable.bv5),100)
        animDrawable1.addFrame(resources.getDrawable(R.drawable.bv6),100)
        animDrawable1.isOneShot=false
        animDrawable2.addFrame(resources.getDrawable(R.drawable.bv1),100)
        animDrawable2.addFrame(resources.getDrawable(R.drawable.bv3),100)
        animDrawable2.addFrame(resources.getDrawable(R.drawable.bv4),100)
        animDrawable2.addFrame(resources.getDrawable(R.drawable.bv5),100)
        animDrawable2.addFrame(resources.getDrawable(R.drawable.bv6),100)
        animDrawable2.addFrame(resources.getDrawable(R.drawable.bv2),100)
        animDrawable2.isOneShot=false
        animDrawable3.addFrame(resources.getDrawable(R.drawable.bv6),100)
        animDrawable3.addFrame(resources.getDrawable(R.drawable.bv2),100)
        animDrawable3.addFrame(resources.getDrawable(R.drawable.bv1),100)
        animDrawable3.addFrame(resources.getDrawable(R.drawable.bv4),100)
        animDrawable3.addFrame(resources.getDrawable(R.drawable.bv3),100)
        animDrawable3.addFrame(resources.getDrawable(R.drawable.bv5),100)
        animDrawable3.isOneShot=false
        animDrawable4.addFrame(resources.getDrawable(R.drawable.bv2),100)
        animDrawable4.addFrame(resources.getDrawable(R.drawable.bv1),100)
        animDrawable4.addFrame(resources.getDrawable(R.drawable.bv4),100)
        animDrawable4.addFrame(resources.getDrawable(R.drawable.bv3),100)
        animDrawable4.addFrame(resources.getDrawable(R.drawable.bv6),100)
        animDrawable4.addFrame(resources.getDrawable(R.drawable.bv5),100)
        animDrawable4.isOneShot=false
        animDrawable5.addFrame(resources.getDrawable(R.drawable.bv5),100)
        animDrawable5.addFrame(resources.getDrawable(R.drawable.bv4),100)
        animDrawable5.addFrame(resources.getDrawable(R.drawable.bv1),100)
        animDrawable5.addFrame(resources.getDrawable(R.drawable.bv6),100)
        animDrawable5.addFrame(resources.getDrawable(R.drawable.bv2),100)
        animDrawable5.addFrame(resources.getDrawable(R.drawable.bv3),100)
        animDrawable5.isOneShot=false

        bv1.background=animDrawable1
        bv2.background=animDrawable2
        bv3.background=animDrawable3
        bv4.background=animDrawable4
        bv5.background=animDrawable5



    }

    private fun initPlayer() {
        gsyPlayer.setUp(HomeCastNew.datas[p].radio_source,true,"嫂子闭眼睛")
        itemTittle.text=HomeCastNew.datas[p].radio_name
        itemDes.text=HomeCastNew.datas[p].online
        Glide.with(this).load(Contents.IMG_URL+HomeCastNew.datas[p].radio_thumb).into(itemImg)
        /***********/

    }

    private fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
        btSwitch.setOnClickListener {
            if (isPlaying==false){
                gsyPlayer.startButton.performClick()
                handler?.sendEmptyMessageDelayed(1,100)
                animator.start()
                if (isLoaded==false){
//                    gsyPlayer.startButton.performClick()
//                    player.prepare()
                }else{
//                    player.start()
                }
                isPlaying=true
                animDrawable1.start()
                animDrawable2.start()
                animDrawable3.start()
                animDrawable4.start()
                animDrawable5.start()
                btSwitch.setImageResource(R.drawable.ic_stop_xq)
            }else{
                gsyPlayer.startButton.performClick()
                animator.pause()
                animDrawable1.stop()
                animDrawable2.stop()
                animDrawable3.stop()
                animDrawable4.stop()
                animDrawable5.stop()
//                player.pause()
                isPlaying=false
                btSwitch.setImageResource(R.drawable.ic_gb_bofang)
            }
        }
        btLast.setOnClickListener {
            if (p>0){
                p--
                initPlayer()
                gsyPlayer.release()
                if (isPlaying){
                    gsyPlayer.startButton.performClick()
                }
            }else{
                Toast.makeText(this@CastDetailAty,"没有上一个了",Toast.LENGTH_SHORT).show()
            }
        }
        btNext.setOnClickListener {
            if (p<HomeCastNew.datas.size-1){
                p++
                initPlayer()
                gsyPlayer.release()
                if (isPlaying){
                    gsyPlayer.startButton.performClick()
                }
            }else{
                Toast.makeText(this@CastDetailAty,"没有下一个了",Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onPause() {
        super.onPause()
        gsyPlayer.onVideoPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        gsyPlayer.release()
        isPlaying=false
    }
    override fun onResume() {
        super.onResume()
        gsyPlayer.onVideoResume()
    }
    override fun onBufferingUpdate(mp: MediaPlayer?, percent: Int) {
    }

    override fun onPrepared(mp: MediaPlayer?) {
        mp?.start()
        isLoaded=true
        isPlaying=true
    }
}
